﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace B07_MusicPlayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> listMusic = new List<string>();
        SoundPlayer player = new SoundPlayer();
        private void Btn_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "音乐文件|*.mp3|所有文件|*.*";
            openFileDialog.InitialDirectory = @"E:\CloudMusic";
            openFileDialog.Title = "选择要添加的音乐文件";
            openFileDialog.Multiselect = true;
            openFileDialog.ShowDialog();

            string[] paths = openFileDialog.FileNames;

            for(int i = 0; i < paths.Length; ++i)
            {
                lbx_music.Items.Add(Path.GetFileName(paths[i]));
                listMusic.Add(paths[i]);
            }
        }

        private void Btn_Previous_Click(object sender, EventArgs e)
        {
            int index = lbx_music.SelectedIndex;
            index--;
            if (index == -1)
                index = listMusic.Count - 1;


            lbx_music.SelectedIndex = index;
            player.SoundLocation = listMusic[lbx_music.SelectedIndex];
            player.Play();
        }

        private void Btn_Next_Click(object sender, EventArgs e)
        {
            int index = lbx_music.SelectedIndex;
            index++;
            if (index == listMusic.Count)
                index = 0;
            lbx_music.SelectedIndex = index;

            player.SoundLocation = listMusic[lbx_music.SelectedIndex];
            player.Play();
        }

        private void Lbx_music_DoubleClick(object sender, EventArgs e)
        {
            player.SoundLocation = listMusic[lbx_music.SelectedIndex];
            player.Play();
        }
    }
}
